using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Configuration;
using dender.dvdcover.core;
using Db4objects.Db4o;
using Db4objects.Db4o.Query;
using System.Drawing.Printing;
using System.Drawing;
using System.Collections;
using System.Diagnostics;

namespace dender.dvdcover.core
{
    /// <summary>
    /// Persistant collection to store the available formats
    /// </summary>
    public class FormatInventory : List<Format>
    {
        string mFilename;

        /// <summary>
        /// Creates an inventory by specifying a filename.
        /// If the file does not exist, the file will be created
        /// If the file do exists, it will be loaded
        /// </summary>
        /// <param name="filename">Full Filename</param>
        public FormatInventory(string filename) : base()
        {
            mFilename = filename;
            FileInfo info = new FileInfo(mFilename);
            if (info.Exists)
            {
                //Load the file
                this.Load();
            }
            else
            {
                //Create the file 
                Initialize();
                Save();
            }
        }


        /// <summary>
        /// Saves the inventory to the specified file
        /// </summary>
        /// <param name="filename">Full filename</param>
        public void Save()
        {
            FileInfo info = new FileInfo(mFilename);
            info.Delete();
            FileStream s = info.Create();
            s.Dispose();

            IObjectContainer db = Db4oFactory.OpenFile(mFilename);
            try
            {
                foreach (Format f in this)
                {
                    db.Set(f);
                }
            }
            finally
            {
                db.Close();
            }

        }

        /// <summary>
        /// Load the inventory from a specified file
        /// </summary>
        /// <param name="filename"></param>
        public void Load()
        {
            IObjectContainer db = Db4oFactory.OpenFile(mFilename);
            try
            {
                this.Clear();
                IList<Format> returnList = db.Query<Format>();
                if (returnList.Count > 0)
                {
                    this.AddRange(returnList);
                }
                else
                {
                    this.Initialize();
                }
            }
            finally
            {
                db.Close();
            }
        }

        internal void Initialize()
        {
            Format f = new Format(274, 184, "Standard A4 DVD Cover (274mm x 184mm)", PageOrientation.Landscape, PaperKind.A4);
            Part mainPart = new Part(new PointF(0, 0), "Main", 274, 184);
            f.AddPart(mainPart);
            f.AddDefaultCuttingLines();
            this.Add(f);

            f = new Format(184,273 , "CD Cover ", PageOrientation.Portrait, PaperKind.A4);
            Part frontPart = new Part(new PointF(0, 0), "Front", 120, 120);
            Part backPart = new Part(new PointF(0, 130), "Back", 151, 120);

            f.AddPart(frontPart);
            f.AddPart(backPart);
            
            this.Add(f);

        }
    }

}
